﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace com.dz.common
{
    public class DESEncrypt
    {
        public static string DefaultKey = "1D16EE4825A24ae0822F819A";
        private static string sKey = "1D16EE4825A24ae0822F819AdF6CDE14";//默认密钥 32位长度
        private static string sIV = "qcDY6X+aPLw="; //默认矢量
        private static SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider(); //构造一个对称算法

   

        #region 属性矢量 public string EnVector
        /// <summary>
        /// 矢量，可以为空
        /// </summary>
        public static string EnVector
        {
            set
            {
                sIV = value;
            }
            get
            {
                return sIV;
            }
        }

        #endregion

        #region 加密方法 public string EncryptString(string Value)
        /// <summary>
        /// 加密方法
        /// </summary>
        /// <param name="Value">要加密的字符串</param>
        /// <returns>返回加密后的字符串</returns>
        public static string EncryptString(string Value)
        {
            string Rv = string.Empty;

            mCSP.Key = Convert.FromBase64String(sKey);
            mCSP.IV = Convert.FromBase64String(sIV);
            //指定加密的运算模式
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            //获取或设置加密算法的填充模式
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            using (ICryptoTransform ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV))
            {
                byte[] byt = Encoding.UTF8.GetBytes(Value);
                using (MemoryStream ms = new MemoryStream())
                {
                    using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
                    {
                        cs.Write(byt, 0, byt.Length);
                        cs.FlushFinalBlock();
                    }
                    Rv = Convert.ToBase64String(ms.ToArray());
                }
            }

            return Rv;
        }
        #endregion

        #region 解密方法 public string DecryptString(string Value)
        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="Value">要解密的字符串</param>
        /// <returns>返回解密后的字符串</returns>
        public static string DecryptString(string Value)
        {
            string Rv = string.Empty;

            mCSP.Key = Convert.FromBase64String(sKey);
            mCSP.IV = Convert.FromBase64String(sIV);
            mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
            mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

            using (ICryptoTransform ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV))
            {
                try
                {
                    byte[] byt = Convert.FromBase64String(Value);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write))
                        {
                            cs.Write(byt, 0, byt.Length);
                            cs.FlushFinalBlock();
                        }
                        Rv = Encoding.UTF8.GetString(ms.ToArray());
                    }
                }
                catch (System.FormatException ex)
                {
                    //屏蔽 “Base-64 字符数组的无效长度”
                }
                catch (System.Security.Cryptography.CryptographicException ex)
                {
                    //屏蔽 “要解密的数据的长度无效”
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex.ToString());
                }
            }

            return Rv;
        }
        #endregion
    }
}
